package com.huohuzhihui.api.service.impl;

import com.alibaba.fastjson.JSON;
import com.huohuzhihui.api.service.ApiDouyinUserService;
import com.huohuzhihui.common.utils.DateUtils;
import com.huohuzhihui.douyin.basis.body.video.AutoCreateVideoParam;
import com.huohuzhihui.douyin.basis.response.user.UserOpenInfoRes;
import com.huohuzhihui.douyin.basis.response.video.VideoCreateRes;
import com.huohuzhihui.douyin.basis.service.interfaces.DyBasisService;
import com.huohuzhihui.douyin.common.enums.ApiPlatform;
import com.huohuzhihui.douyin.common.response.auth.AccessTokenRes;
import com.huohuzhihui.douyin.domain.DouyinUser;
import com.huohuzhihui.douyin.service.IDouyinUserService;
import com.huohuzhihui.framework.web.service.TokenService;
import com.huohuzhihui.merchant.domain.MerActivityVideo;
import com.huohuzhihui.merchant.domain.MerCouponActivity;
import com.huohuzhihui.merchant.service.IMerActivityVideoService;
import com.huohuzhihui.system.service.ISysConfigService;
import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

import static com.huohuzhihui.douyin.common.enums.ApiPlatform.DOU_YIN;

/**
 * 订单Service业务层处理
 * 
 * @author huohuzhihui
 * @date 2020-11-15
 */
@Service
public class ApiDouyinUserServiceImpl implements ApiDouyinUserService {
    @Resource
    private DyBasisService dyBasisService;
    @Autowired
    private IDouyinUserService douyinUserService;

    @Override
    public DouyinUser queryDouyinUserByCode(String code) {
        //查询抖音AccessToken
        AccessTokenRes.AccessTokenResData accessTokenResData = dyBasisService.getAuth2Service().authAccessToken(code, DOU_YIN);
        //查询抖音用户开放信息
        UserOpenInfoRes.GetUserOpenInfoResData userOpenInfoResData = dyBasisService.getUserManagerService().getUserOpenInfo(accessTokenResData.getOpenId(), DOU_YIN);

        DouyinUser douyinUser = new DouyinUser();
        douyinUser.setOpenId(userOpenInfoResData.getOpenId());
        douyinUser.setUnionId(userOpenInfoResData.getUnionId());
        douyinUser.setAvatar(userOpenInfoResData.getAvatar());
        douyinUser.setCity(userOpenInfoResData.getCity());
        douyinUser.setCountry(userOpenInfoResData.getCountry());
        douyinUser.setGender(userOpenInfoResData.getGender());
        douyinUser.setNickname(userOpenInfoResData.getNickName());
        douyinUser.setProvince(userOpenInfoResData.getProvince());

        return douyinUser;
    }

    @Override
    public DouyinUser saveDouyinUserByCode(String code) {
        //查询抖音AccessToken
        AccessTokenRes.AccessTokenResData accessTokenResData = dyBasisService.getAuth2Service().authAccessToken(code, DOU_YIN);
        DouyinUser douyinUser = this.douyinUserService.selectDouyinUserByOpenId(accessTokenResData.getOpenId());
        if(douyinUser==null){//如果人员不存在那么进行保存
            UserOpenInfoRes.GetUserOpenInfoResData userOpenInfoResData = dyBasisService.getUserManagerService().getUserOpenInfo(accessTokenResData.getOpenId(), DOU_YIN);
            douyinUser = new DouyinUser();
            douyinUser.setOpenId(userOpenInfoResData.getOpenId());
            douyinUser.setUnionId(userOpenInfoResData.getUnionId());
            douyinUser.setAvatar(userOpenInfoResData.getAvatar());
            douyinUser.setCity(userOpenInfoResData.getCity());
            douyinUser.setCountry(userOpenInfoResData.getCountry());
            douyinUser.setGender(userOpenInfoResData.getGender());
            douyinUser.setNickname(userOpenInfoResData.getNickName());
            douyinUser.setProvince(userOpenInfoResData.getProvince());
            douyinUser.setCreateTime(DateUtils.getNowDate());
            this.douyinUserService.insertDouyinUser(douyinUser);
        }
        return douyinUser;
    }

}

